package com.xyd.Servlet;

import com.xyd.Service.ChildService;
import com.xyd.Service.ParentService;
import com.xyd.bean.ChildCategory;
import com.xyd.bean.ParentCategory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet({"/admin/sortParentDetail","/admin/parentModify","/admin/getSort","/admin/parentDel",
            "/admin/sortChildDetail","/admin/childModify","/admin/childDel","/admin/addChild"})
public class AdminManageCategoryServlet extends HttpServlet {
    ParentService parentService = new ParentService();
    ChildService childService = new ChildService();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String path = request.getServletPath();
       if (("/admin/sortParentDetail").equals(path)){
           doParentDetail(request,response);
       }else if ("/admin/parentModify".equals(path)){
           doParentModify(request,response);
        }else if ("/admin/getSort".equals(path)){
           doGetSort(request,response);
       }else if ("/admin/parentDel".equals(path)){
           doDelParent(request,response);
       }else if ("/admin/sortChildDetail".equals(path)){
           doChildDetail(request,response);
       }else if ("/admin/childModify".equals(path)){
           doChildModify(request,response);
       }else if ("/admin/childDel".equals(path)){
           doDelChild(request,response);
       }else if ("/admin/addChild".equals(path)){
           doAddChild(request,response);
       }
    }

    private void doAddChild(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String parentId = request.getParameter("parentId");
        String childname = request.getParameter("childname");
        ParentCategory parentCategory = new ParentCategory();
        ChildCategory childCategory = new ChildCategory();
        parentCategory.setParentid(Integer.parseInt(parentId));
        childCategory.setParentCategory(parentCategory);
        childCategory.setChildname(childname);
        int i = childService.addChild(childCategory);
        if (i > 0){
            response.sendRedirect("/admin/getSort");
        }
    }

    private void doDelChild(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String childid = request.getParameter("childid");
        int i = childService.delChild(Integer.parseInt(childid));
        if (i > 0){
            response.sendRedirect("/admin/getSort");
        }
    }

    private void doChildModify(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String childid = request.getParameter("childid");
        String childname = request.getParameter("childname");
        String parentid = request.getParameter("parentid");
        ParentCategory parentCategory= new ParentCategory();
        ChildCategory childCategory = new ChildCategory();
        parentCategory.setParentid(Integer.parseInt(parentid));
        childCategory.setChildid(Integer.parseInt(childid));
        childCategory.setChildname(childname);
        childCategory.setParentCategory(parentCategory);
        int i = childService.modifyChild(childCategory);
        if (i > 0){
            response.sendRedirect("/admin/getSort");
        }
    }

    private void doChildDetail(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String childid = request.getParameter("childid");
        ChildCategory childCategory = childService.getById(Integer.parseInt(childid));
        if (childCategory !=null){
            request.setAttribute("childCategory",childCategory);
            request.getRequestDispatcher("/manage/productClass-modify.jsp").forward(request,response);
        }
    }



    /**
     * @title 根据id删除一级父类
     * @description
     * @author admin
     * @param
     * @updateTime 2022/4/22 19:47
     * @return
     */
    private void doDelParent(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String parentid = request.getParameter("parentid");
        int i = parentService.delParent(Integer.parseInt(parentid));
        if (i >0){
            response.sendRedirect("/admin/getSort");
        }
    }

    /**
     * @title 修改以后调用此方法重新查询数据库
     * @description
     * @author admin
     * @param
     * @updateTime 2022/4/22 19:34
     * @return
     */
    private void doGetSort(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<ParentCategory> list = parentService.getAllParent();
        HashMap<ParentCategory, List<ChildCategory>> map = new HashMap<>();
        for (ParentCategory parentCategory : list) {
            List<ChildCategory> childCategory = childService.getChildByPId(parentCategory.getParentid());
            map.put(parentCategory,childCategory);
        }
        request.setAttribute("map", map);
        request.getRequestDispatcher("/manage/productClass.jsp").forward(request,response);
    }

    /**
     * @title 修改一级分类
     * @description
     * @author admin
     * @param
     * @updateTime 2022/4/22 19:25
     * @return
     */

    private void doParentModify(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String parentid = request.getParameter("parentid");
        String parentname = request.getParameter("parentname");
        ParentCategory parentCategory = new ParentCategory();
        parentCategory.setParentid(Integer.parseInt(parentid));
        parentCategory.setParentname(parentname);
        int i = parentService.modifyParent(parentCategory);
        if (i>0){
            response.sendRedirect("/admin/getSort");
        }
    }

    /**
     * @title
     * @description 根据id得到一级分类的详情
     * @author admin
     * @param
     * @updateTime 2022/4/22 18:03
     * @return
     */
    private void doParentDetail(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String parentid = request.getParameter("parentid");
        ParentCategory parentCategory = parentService.getByParentid(Integer.parseInt(parentid));
        if (parentCategory !=null){
            request.setAttribute("parentCategory",parentCategory);
            request.getRequestDispatcher("/manage/productParent-modify.jsp").forward(request,response);
        }
    }

}
